#ifndef SERVERCOMMAND_H
#define SERVERCOMMAND_H

#include "qtlibs.h"
#include "db/db.h"

class ServerCommand {
public:
    //ServerCommand();
    virtual QDomDocument run(QDomDocument doc) = 0;

    bool isLoginCorrect(QString login) {
        QRegExp loginRegExp("^[0-9a-zA-Z#_-&%]{4,255}$");
        return login.contains(loginRegExp);
    }

    bool isHashCorrect(QString hash) {
        QRegExp hashRegExp("^[a-zA-Z0-9]{32}$");
        return hash.contains(hashRegExp);
    }

    bool isAuthorized(QDomDocument doc) {
        return this->getCurrentUser(doc) != NULL;
    }

    User* getCurrentUser(QDomDocument doc) {
        QDomNodeList identifyNodes = doc.elementsByTagName("identify");
        if (identifyNodes.count() == 1) {
            QString identify = identifyNodes.at(0).toElement().text().trimmed();
            return DB::getInstance()->findByIdentify(identify);
        }
        return NULL;
    }
};

#endif // SERVERCOMMAND_H
